package com.example.ncov_management_system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.ncov_management_system.entity.Clbum;
import com.example.ncov_management_system.entity.Login;
import com.example.ncov_management_system.entity.Message;
import com.example.ncov_management_system.entity.Student;
import com.example.ncov_management_system.form.StudentForm;
import com.example.ncov_management_system.service.*;
import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClbumService clbumService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private RecordService recordService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(name = "pn", defaultValue = "0") int pn, String name, String clbum) {
        model.addAttribute("page", studentService.list(pn, name, clbum));
        model.addAttribute("clbums", clbumService.list());
        return "student_list";
    }

    @GetMapping("/to_add")
    public String toAdd(Model model) {
        model.addAttribute("clbums", clbumService.list());
        return "student_add";
    }

    @PostMapping("/add")
    public String add(StudentForm studentForm) {
        log.info("student:{}", studentForm);
        Clbum clbum_db = clbumService.findByName(studentForm.getClbum());
        Student student = new Student();
        student.setName(studentForm.getName());
        student.setSex(studentForm.getSex());
        student.setBirthday(studentForm.getBirthday());
        student.setPhone(studentForm.getPhone());
        student.setEmail(studentForm.getEmail());
        student.setClbum(clbum_db);
        studentService.add(student);
        return "redirect:/student/list";
    }

    @PostMapping("/edit")
    public String edit(StudentForm studentForm) {
        log.info("student:{}", studentForm);
        Clbum clbum_db = clbumService.findByName(studentForm.getClbum());
        Student student = new Student();
        student.setId(studentForm.getId());
        student.setName(studentForm.getName());
        student.setSex(studentForm.getSex());
        student.setBirthday(studentForm.getBirthday());
        student.setPhone(studentForm.getPhone());
        student.setEmail(studentForm.getEmail());
        student.setClbum(clbum_db);
        studentService.update(student);
        return "redirect:/student/list";
    }

    @GetMapping("/{id}")
    public String edit(@PathVariable long id, Model model) {
        Student student = studentService.findById(id);
        model.addAttribute("student", student);
        model.addAttribute("clbums", clbumService.list());
        return "student_edit";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam long id) {
        studentService.delete(id);
        return "redirect:/student/list";
    }

    @GetMapping("/index")
    public String adminIndex(HttpSession session,Model model) {
        Student student = (Student) session.getAttribute("student");
        List<Message> messages = messageService.findUnReadByStudentId(student.getId());
        session.setAttribute("messages",messages);
        session.setAttribute("message_count",messages.size());
        Student student1 = studentService.findById(student.getId());
        Long id = student1.getClbum().getId();
        List<Message> messages1 = student1.getMessages();
        List<Student> studentAll = studentService.findByClbumId(id);
        List<Student> studentsReported = recordService.findReportedStudentByClbumId(id);
        model.addAttribute("student_reported", studentsReported.size());
        model.addAttribute("student_un_reported", studentAll.size()-studentsReported.size());
        model.addAttribute("student_message", messages1.size());
        return "student_index";
    }
}
